package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.domain.ShoppingCart;

import com.itheima.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @GetMapping("/list")
    public R<List> list() {
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(wrapper);
        return R.success(shoppingCartList);
    }

    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        log.info("shoppingCart:{}", shoppingCart);
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        Long dishId = shoppingCart.getDishId();
        if (dishId != null) {
            //菜品
            wrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {
            //套餐
            wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        ShoppingCart shopping = shoppingCartService.getOne(wrapper);
        if (shopping != null) {
            //购物车有
            Integer number = shopping.getNumber();
            shopping.setNumber(number + 1);
            shoppingCartService.updateById(shopping);
        } else {
            //购物车没有
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCartService.save(shoppingCart);
            shopping = shoppingCart;
        }
        return R.success(shopping);
    }

    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart) {
        log.info("shoppingCart:{}", shoppingCart);
        Long dishId = shoppingCart.getDishId();
        if (dishId != null) {
            LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ShoppingCart::getDishId, dishId);
            ShoppingCart dishShoppingCart = shoppingCartService.getOne(wrapper);
            Integer number = dishShoppingCart.getNumber();
            dishShoppingCart.setNumber(number - 1);
            if (number == 1) {
                Long dishShoppingCartId = dishShoppingCart.getId();
                shoppingCartService.removeById(dishShoppingCartId);
                return R.success(dishShoppingCart);
            }
            shoppingCartService.updateById(dishShoppingCart);
            return R.success(dishShoppingCart);
        } else {
            LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
            ShoppingCart setmealShoppingCart = shoppingCartService.getOne(wrapper);
            Integer number = setmealShoppingCart.getNumber();
            setmealShoppingCart.setNumber(number - 1);
            if (number == 1) {
                Long setmealShoppingCartId = setmealShoppingCart.getId();
                shoppingCartService.removeById(setmealShoppingCartId);
                return R.success(setmealShoppingCart);
            }
            shoppingCartService.updateById(setmealShoppingCart);
            return R.success(setmealShoppingCart);
        }
    }

    @DeleteMapping("/clean")
    public R<String> delete() {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        shoppingCartService.remove(queryWrapper);
        return R.success("清空购物车成功");
    }

}